package com.fhz.a1909;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;
import androidx.work.Constraints;
import androidx.work.Data;
import androidx.work.NetworkType;
import androidx.work.OneTimeWorkRequest;
import androidx.work.Operation;
import androidx.work.PeriodicWorkRequest;
import androidx.work.WorkInfo;
import androidx.work.WorkManager;

import com.fhz.a1909.work.MyWork;

import java.util.concurrent.TimeUnit;

import javax.inject.Inject;
import javax.inject.Named;

import dagger.hilt.android.AndroidEntryPoint;

@AndroidEntryPoint
public class MainActivity extends AppCompatActivity {
    MyUtils myUtils;
    private static final String TAG = "feng";

//    @Inject
//    @Named("user")
//    Person user;

    @Inject
    User user;

    @Inject
    @Named("student")
    Person student;


    private Button add;
    private Button addWork;

    private TextView num;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.i(TAG, "onCreate: ");
        myUtils = new MyUtils();
        getLifecycle().addObserver(myUtils);

        addWork = (Button) findViewById(R.id.addWork);


        System.out.println(user.getName());
        student.eat();

        addWork.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
            }
        });
        //运行的约束条件
        Constraints constraints = new Constraints.Builder()
                .setRequiredNetworkType(NetworkType.CONNECTED)
                .build();

        //执行一次的任务
        OneTimeWorkRequest build = new OneTimeWorkRequest.Builder(MyWork.class)
                .setConstraints(constraints)
                .setInitialDelay(3, TimeUnit.SECONDS)
                .setInputData(new Data.Builder().putString("xiaoming","男").build())
                .build();
        PeriodicWorkRequest build1 = new PeriodicWorkRequest.Builder(MyWork.class, 15, TimeUnit.SECONDS).build();

        //任务管理

        WorkManager.getInstance(MainActivity.this).enqueue(build);



        //观察任务
        WorkManager.getInstance(this).getWorkInfoByIdLiveData(build.getId()).observe(this, new Observer<WorkInfo>() {
            @Override
            public void onChanged(WorkInfo workInfo) {
                Log.i(TAG, "onChanged: "+workInfo);
            }
        });

        add = (Button) findViewById(R.id.add);
        num = (TextView) findViewById(R.id.num);


    //        String key = savedInstanceState.getString("key");
    //        if(key != null){
    //            num.setText(key);
    //        }


        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String s = num.getText().toString();
                int i = Integer.parseInt(s);
                num.setText(String.valueOf(++i));
            }
        });


    }

//
//    @Override
//    protected void onSaveInstanceState(@NonNull Bundle outState) {
//        super.onSaveInstanceState(outState);
//        outState.putString("key",num.getText().toString());
//    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.i(TAG, "onResume: ");
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.i(TAG, "onStart: ");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.i(TAG, "onDestroy: ");
    }


}